﻿using Common.Infrastructure.Logs;
using Common.Infrastructure.Utils;
using System;

namespace Common.Infrastructure.Service
{
    public class LogService : ILogService
    {
        private readonly IComonCacheService comonCacheService;
        private long logTick;
        private string logservicekey;

        public LogService(IComonCacheService comonCache)
        {
            this.comonCacheService = comonCache;
            this.logTick = DateTime.Now.Ticks;
            this.logservicekey = Guid.NewGuid().ToString().Replace("-", string.Empty);
        }

        public void AddError(string message)
        {
            DoAddLog("Error", message, null, null, null, null, 0);
        }

        public void AddError(string message, string logMsg, object obj)
        {
            DoAddLog("Error", message, null, obj?.ToJsonWithoutQuot(), logMsg, null, 0);
        }

        public void AddError(string code, params object[] objs)
        {
            var m = this.GetMessage(code, objs);
            AddError(m);
        }

        public void AddError(string message, Exception exception)
        {
            DoAddLog("Error", message, LibExceptionUtils.GetError(exception)?.StackTrace, null, null, null, 0);
        }

        public void AddWarning(string message)
        {
            DoAddLog("Warning", message, null, null, null, null, 0);
        }

        public void AddWarning(string code, params object[] objs)
        {
            var m = this.GetMessage(code, objs);
            AddWarning(m);
        }

        public void AddInfomation(string message)
        {
            DoAddLog("Infomation", message, null, null, null, null, 0);
        }

        public void AddInfomation(string code, params object[] objs)
        {
            var m = this.GetMessage(code, objs);
            AddInfomation(m);
        }

        public void AddInfomation(string message, string logMsg)
        {
            DoAddLog("Infomation", message, null, null, logMsg, null, 0);
        }

        public void AddInfomation(string code, string logMsg, params object[] objs)
        {
            var m = this.GetMessage(code, objs);
            AddInfomation(m, logMsg);
        }

        public void AddInfomation(string message, object obj, string logMsg)
        {
            DoAddLog("Infomation", message, null, obj?.ToJsonWithoutQuot(), logMsg, null, 0);
        }

        public void AddInfomation(string code, object obj, string logMsg, params object[] objs)
        {
            var m = this.GetMessage(code, objs);
            AddInfomation(m, logMsg, obj);
        }

        public void AddInfomation(string message, object obj, string logMsg, object logMsgObj)
        {
            DoAddLog("Infomation", message, null, obj?.ToJsonWithoutQuot(), logMsg, logMsgObj?.ToJsonWithoutQuot(), 0);
        }

        public void AddInfomation(string message, object obj, string logMsg, object logMsgObj, long startLogTick)
        {
            DoAddLog("Infomation", message, null, obj?.ToJsonWithoutQuot(), logMsg, logMsgObj?.ToJsonWithoutQuot(), startLogTick);
        }

        public void AddLog(string message, string obj, string logMsg, string logMsgObj)
        {
            DoAddLog("Infomation", message, null, obj, logMsg, logMsgObj, 0);
        }

        private string GetMessage(string code, params object[] objs)
        {
            var foramt = MessageStorageUtils.GetMessageFormat(code, null);
            return string.Format(foramt, objs);
        }

        private void DoAddLog(string type, string message, string trace, string paramObj, string logMsg, string logMsgParam, long startLogTick)
        {
            var ti = DateTime.Now.Ticks;
            if (startLogTick == 0)
            {
                startLogTick = this.logTick;
            }

            var s = new SysLogModel()
            {
                ApiKey = this.comonCacheService.GetApiKey(),
                ServiceCode = this.comonCacheService.GetServiceCode(),
                Msg = message,
                Type = type,
                MsgTrace = trace,
                CostTick = ti - startLogTick,
                CTTick = logTick,
                LogKey = this.logservicekey,
                LogMsgParamObj = logMsgParam,
                LogMsg = logMsg,
                ParamObj = paramObj,
            };
            this.logTick = ti;

            _ = WebApiUtils.PostByResponse($"{this.comonCacheService.GetGroupUrl()}/api/Log/SysLog/AddSysLog", s);
        }
    }
}
